﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Customer_AllotLogs
    /// </summary>
    public class Customer_AllotLogs
    {
        private const string CONNECTSTRING_SEARCH = "Chenxin_House_Search";
        private const string CONNECTSTRING = "Chenxin_House";

        public Customer_AllotLogs()
        { }

        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordID)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer_AllotLogs where RecordID=@RecordID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Customer_AllotLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Customer_AllotLogs(");
            strSql.Append("RecordID,CustomerId,CustomerCode,CustomerName,OldPersonID,OldPerson,OldCityId,OldCity,OldCompanyId,OldCompany,OldDepartID,OldDepart,NowPersonID,NowPerson,NowCityId,NowCity,NowCompanyId,NowCompany,NowDepartID,NowDepart,OperPersonID,OperPerson,OperCityId,OperCity,OperCompanyId,OperCompany,OperDepartId,OperDepart,OperTime)");

            strSql.Append(" values (");
            strSql.Append("@RecordID,@CustomerId,@CustomerCode,@CustomerName,@OldPersonID,@OldPerson,@OldCityId,@OldCity,@OldCompanyId,@OldCompany,@OldDepartID,@OldDepart,@NowPersonID,@NowPerson,@NowCityId,@NowCity,@NowCompanyId,@NowCompany,@NowDepartID,@NowDepart,@OperPersonID,@OperPerson,@OperCityId,@OperCity,@OperCompanyId,@OperCompany,@OperDepartId,@OperDepart,@OperTime)");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, model.RecordID);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerCode", DbType.AnsiString, model.CustomerCode);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "OldPersonID", DbType.Guid, model.OldPersonID);
            db.AddInParameter(dbCommand, "OldPerson", DbType.AnsiString, model.OldPerson);
            db.AddInParameter(dbCommand, "OldCityId", DbType.Guid, model.OldCityId);
            db.AddInParameter(dbCommand, "OldCity", DbType.AnsiString, model.OldCity);
            db.AddInParameter(dbCommand, "OldCompanyId", DbType.Guid, model.OldCompanyId);
            db.AddInParameter(dbCommand, "OldCompany", DbType.AnsiString, model.OldCompany);
            db.AddInParameter(dbCommand, "OldDepartID", DbType.Guid, model.OldDepartID);
            db.AddInParameter(dbCommand, "OldDepart", DbType.AnsiString, model.OldDepart);
            db.AddInParameter(dbCommand, "NowPersonID", DbType.Guid, model.NowPersonID);
            db.AddInParameter(dbCommand, "NowPerson", DbType.AnsiString, model.NowPerson);
            db.AddInParameter(dbCommand, "NowCityId", DbType.Guid, model.NowCityId);
            db.AddInParameter(dbCommand, "NowCity", DbType.AnsiString, model.NowCity);
            db.AddInParameter(dbCommand, "NowCompanyId", DbType.Guid, model.NowCompanyId);
            db.AddInParameter(dbCommand, "NowCompany", DbType.AnsiString, model.NowCompany);
            db.AddInParameter(dbCommand, "NowDepartID", DbType.Guid, model.NowDepartID);
            db.AddInParameter(dbCommand, "NowDepart", DbType.AnsiString, model.NowDepart);
            db.AddInParameter(dbCommand, "OperPersonID", DbType.Guid, model.OperPersonID);
            db.AddInParameter(dbCommand, "OperPerson", DbType.AnsiString, model.OperPerson);
            db.AddInParameter(dbCommand, "OperCityId", DbType.Guid, model.OperCityId);
            db.AddInParameter(dbCommand, "OperCity", DbType.AnsiString, model.OperCity);
            db.AddInParameter(dbCommand, "OperCompanyId", DbType.Guid, model.OperCompanyId);
            db.AddInParameter(dbCommand, "OperCompany", DbType.AnsiString, model.OperCompany);
            db.AddInParameter(dbCommand, "OperDepartId", DbType.Guid, model.OperDepartId);
            db.AddInParameter(dbCommand, "OperDepart", DbType.AnsiString, model.OperDepart);
            db.AddInParameter(dbCommand, "OperTime", DbType.DateTime, model.OperTime);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Customer_AllotLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Customer_AllotLogs set ");
            strSql.Append("CustomerId=@CustomerId,");
            strSql.Append("CustomerCode=@CustomerCode,");
            strSql.Append("CustomerName=@CustomerName,");
            strSql.Append("OldPersonID=@OldPersonID,");
            strSql.Append("OldPerson=@OldPerson,");
            strSql.Append("OldCityId=@OldCityId,");
            strSql.Append("OldCity=@OldCity,");
            strSql.Append("OldCompanyId=@OldCompanyId,");
            strSql.Append("OldCompany=@OldCompany,");
            strSql.Append("OldDepartID=@OldDepartID,");
            strSql.Append("OldDepart=@OldDepart,");
            strSql.Append("NowPersonID=@NowPersonID,");
            strSql.Append("NowPerson=@NowPerson,");
            strSql.Append("NowCityId=@NowCityId,");
            strSql.Append("NowCity=@NowCity,");
            strSql.Append("NowCompanyId=@NowCompanyId,");
            strSql.Append("NowCompany=@NowCompany,");
            strSql.Append("NowDepartID=@NowDepartID,");
            strSql.Append("NowDepart=@NowDepart,");
            strSql.Append("OperPersonID=@OperPersonID,");
            strSql.Append("OperPerson=@OperPerson,");
            strSql.Append("OperCityId=@OperCityId,");
            strSql.Append("OperCity=@OperCity,");
            strSql.Append("OperCompanyId=@OperCompanyId,");
            strSql.Append("OperCompany=@OperCompany,");
            strSql.Append("OperDepartId=@OperDepartId,");
            strSql.Append("OperDepart=@OperDepart,");
            strSql.Append("OperTime=@OperTime");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, model.RecordID);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerCode", DbType.AnsiString, model.CustomerCode);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "OldPersonID", DbType.Guid, model.OldPersonID);
            db.AddInParameter(dbCommand, "OldPerson", DbType.AnsiString, model.OldPerson);
            db.AddInParameter(dbCommand, "OldCityId", DbType.Guid, model.OldCityId);
            db.AddInParameter(dbCommand, "OldCity", DbType.AnsiString, model.OldCity);
            db.AddInParameter(dbCommand, "OldCompanyId", DbType.Guid, model.OldCompanyId);
            db.AddInParameter(dbCommand, "OldCompany", DbType.AnsiString, model.OldCompany);
            db.AddInParameter(dbCommand, "OldDepartID", DbType.Guid, model.OldDepartID);
            db.AddInParameter(dbCommand, "OldDepart", DbType.AnsiString, model.OldDepart);
            db.AddInParameter(dbCommand, "NowPersonID", DbType.Guid, model.NowPersonID);
            db.AddInParameter(dbCommand, "NowPerson", DbType.AnsiString, model.NowPerson);
            db.AddInParameter(dbCommand, "NowCityId", DbType.Guid, model.NowCityId);
            db.AddInParameter(dbCommand, "NowCity", DbType.AnsiString, model.NowCity);
            db.AddInParameter(dbCommand, "NowCompanyId", DbType.Guid, model.NowCompanyId);
            db.AddInParameter(dbCommand, "NowCompany", DbType.AnsiString, model.NowCompany);
            db.AddInParameter(dbCommand, "NowDepartID", DbType.Guid, model.NowDepartID);
            db.AddInParameter(dbCommand, "NowDepart", DbType.AnsiString, model.NowDepart);
            db.AddInParameter(dbCommand, "OperPersonID", DbType.Guid, model.OperPersonID);
            db.AddInParameter(dbCommand, "OperPerson", DbType.AnsiString, model.OperPerson);
            db.AddInParameter(dbCommand, "OperCityId", DbType.Guid, model.OperCityId);
            db.AddInParameter(dbCommand, "OperCity", DbType.AnsiString, model.OperCity);
            db.AddInParameter(dbCommand, "OperCompanyId", DbType.Guid, model.OperCompanyId);
            db.AddInParameter(dbCommand, "OperCompany", DbType.AnsiString, model.OperCompany);
            db.AddInParameter(dbCommand, "OperDepartId", DbType.Guid, model.OperDepartId);
            db.AddInParameter(dbCommand, "OperDepart", DbType.AnsiString, model.OperDepart);
            db.AddInParameter(dbCommand, "OperTime", DbType.DateTime, model.OperTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer_AllotLogs ");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_AllotLogs GetModel(Guid RecordID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CustomerId,CustomerCode,CustomerName,OldPersonID,OldPerson,OldCityId,OldCity,OldCompanyId,OldCompany,OldDepartID,OldDepart,NowPersonID,NowPerson,NowCityId,NowCity,NowCompanyId,NowCompany,NowDepartID,NowDepart,OperPersonID,OperPerson,OperCityId,OperCity,OperCompanyId,OperCompany,OperDepartId,OperDepart,OperTime from Customer_AllotLogs ");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            Chenxin.Lib.House.Entity.Customer_AllotLogs model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CustomerId,CustomerCode,CustomerName,OldPersonID,OldPerson,OldCityId,OldCity,OldCompanyId,OldCompany,OldDepartID,OldDepart,NowPersonID,NowPerson,NowCityId,NowCity,NowCompanyId,NowCompany,NowDepartID,NowDepart,OperPersonID,OperPerson,OperCityId,OperCity,OperCompanyId,OperCompany,OperDepartId,OperDepart,OperTime ");
            strSql.Append(" FROM Customer_AllotLogs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Customer_AllotLogs> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CustomerId,CustomerCode,CustomerName,OldPersonID,OldPerson,OldCityId,OldCity,OldCompanyId,OldCompany,OldDepartID,OldDepart,NowPersonID,NowPerson,NowCityId,NowCity,NowCompanyId,NowCompany,NowDepartID,NowDepart,OperPersonID,OperPerson,OperCityId,OperCity,OperCompanyId,OperCompany,OperDepartId,OperDepart,OperTime ");
            strSql.Append(" FROM Customer_AllotLogs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Customer_AllotLogs> list = new List<Chenxin.Lib.House.Entity.Customer_AllotLogs>();
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_AllotLogs ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Customer_AllotLogs model = new Chenxin.Lib.House.Entity.Customer_AllotLogs();
            object ojb;
            ojb = dataReader["RecordID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordID = new Guid(ojb.ToString());
            }
            ojb = dataReader["CustomerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CustomerId = new Guid(ojb.ToString());
            }
            model.CustomerCode = dataReader["CustomerCode"].ToString();
            model.CustomerName = dataReader["CustomerName"].ToString();
            ojb = dataReader["OldPersonID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OldPersonID = new Guid(ojb.ToString());
            }
            model.OldPerson = dataReader["OldPerson"].ToString();
            ojb = dataReader["OldCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OldCityId = new Guid(ojb.ToString());
            }
            model.OldCity = dataReader["OldCity"].ToString();
            ojb = dataReader["OldCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OldCompanyId = new Guid(ojb.ToString());
            }
            model.OldCompany = dataReader["OldCompany"].ToString();
            ojb = dataReader["OldDepartID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OldDepartID = new Guid(ojb.ToString());
            }
            model.OldDepart = dataReader["OldDepart"].ToString();
            ojb = dataReader["NowPersonID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NowPersonID = new Guid(ojb.ToString());
            }
            model.NowPerson = dataReader["NowPerson"].ToString();
            ojb = dataReader["NowCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NowCityId = new Guid(ojb.ToString());
            }
            model.NowCity = dataReader["NowCity"].ToString();
            ojb = dataReader["NowCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NowCompanyId = new Guid(ojb.ToString());
            }
            model.NowCompany = dataReader["NowCompany"].ToString();
            ojb = dataReader["NowDepartID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NowDepartID = new Guid(ojb.ToString());
            }
            model.NowDepart = dataReader["NowDepart"].ToString();
            ojb = dataReader["OperPersonID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperPersonID = new Guid(ojb.ToString());
            }
            model.OperPerson = dataReader["OperPerson"].ToString();
            ojb = dataReader["OperCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperCityId = new Guid(ojb.ToString());
            }
            model.OperCity = dataReader["OperCity"].ToString();
            ojb = dataReader["OperCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperCompanyId = new Guid(ojb.ToString());
            }
            model.OperCompany = dataReader["OperCompany"].ToString();
            ojb = dataReader["OperDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperDepartId = new Guid(ojb.ToString());
            }
            model.OperDepart = dataReader["OperDepart"].ToString();
            ojb = dataReader["OperTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion

    }
}
